#include <iostream>
#define ll long long

using namespace std;

int main(){

	int n,d;
	cin >> n >> d;
	ll a[n];
	for(int i = 0; i < n; ++i){
		cin >> a[i];
	}
	
	ll ans = 0;
	
	for(int i = 0; i < n - 2; ++i){
		ll num = a[i] + d;
		int left = i,right = n-1;
		while(left <= right){
			ll mid = left + (right - left)/2;
			if(a[mid] > num){
				right = mid - 1;
			}else{
				left = mid + 1;
			}
		}
		
		int k = right - i;
		if(k >= 2){
			ans += k * (k - 1) / 2;
		}
	}
	
	cout << ans;
	
	return 0;
}

